package com.malty.rocketmq.manager;

import com.malty.rocketmq.constant.MqTopicConst;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.*;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author malty
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class RocketMQManager {
    private final RocketMQTemplate rocketMQTemplate;
    private final AtomicInteger atomicInteger = new AtomicInteger(0);


    public void sendMsg(String msg) {
        SendResult sendResult = rocketMQTemplate.syncSend(MqTopicConst.JAVA_CLIENT_TEST, msg, 8000);
        log.info("发送消息完成, 消息id:{}, 发送结果:{}", sendResult.getMsgId(), sendResult.getSendStatus());
    }

    public void sendMsgOneWay(String msg) {
        rocketMQTemplate.sendOneWay(MqTopicConst.JAVA_CLIENT_TEST, msg);
        log.info("发送单向消息完成, 消息内容:{}", msg);
    }

    public void sendTxMsg(String msg) {
        Message<String> message = MessageBuilder.withPayload(msg).build();
        TransactionSendResult sendResult = rocketMQTemplate.sendMessageInTransaction("tx_test", "java-client-test-tx", message, null);
        SendStatus sendStatus = sendResult.getSendStatus();
        LocalTransactionState localTransactionState = sendResult.getLocalTransactionState();
        log.info("sendStatus:{}, localTransactionState:{}", sendStatus, localTransactionState);
    }

    public void sendDelayMsg(String msg) {
        msg = msg + "_" + System.currentTimeMillis();
        Message<String> message = MessageBuilder.withPayload(msg).build();
        SendResult sendResult = rocketMQTemplate.syncSend(MqTopicConst.JAVA_CLIENT_TEST, message, 1000, 1);
        log.info("发送延时消息完成, 消息内容:{}, 发送结果:{}", msg, sendResult.getSendStatus());
    }

    public void sendSyncOrderMsg(String msg) {
        msg = msg + "_" + atomicInteger.incrementAndGet();
        SendResult sendResult = rocketMQTemplate.syncSendOrderly(MqTopicConst.JAVA_CLIENT_TEST, msg, msg.toString());
        log.info("发送延时消息完成, 消息内容:{}, 发送结果:{}", msg, sendResult.getSendStatus());
    }

    public void sendSyncOrderMsgAsync(String msg) {
        msg = msg + "_" + atomicInteger.incrementAndGet();
        String finalMsg = msg;
        rocketMQTemplate.asyncSendOrderly(MqTopicConst.JAVA_CLIENT_TEST, msg, msg.toString(), new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
//                log.info("异步发送顺序消息完成, 消息内容:{}, 发送结果:{}", finalMsg, sendResult.getSendStatus());
            }

            @Override
            public void onException(Throwable throwable) {
                log.info("异步发送顺序消息失败, expMsg:{}", throwable.getMessage());
            }
        });
    }
}
